package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.AlumnoBean;
import beans.PadreBean;
import dao.interfaces.AlumnoDao;
import dao.interfaces.PadreDao;
import daofactory.DaoFactory;

/**
 * Servlet implementation class BuscarApoderado
 */
@WebServlet("/BuscarApoderado")
public class BuscarApoderado extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuscarApoderado() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		
		
		PrintWriter out = response.getWriter();
		System.out.println("entrando 1");
		try {
			System.out.println("INGRESA");
			String cod=request.getParameter("codigo");
			String nom=request.getParameter("nombre");
			String pat=request.getParameter("apepat");
			String mat=request.getParameter("apemat");
			String gra=request.getParameter("grado");
			String sec=request.getParameter("secc");
			String dni=request.getParameter("dni");
			
			/*genera id del padre*/
			String inicial=pat.substring(0,2);
			String idPer=dni+inicial;
			/*fin del genera id del padre*/
			
			DaoFactory dao = DaoFactory.getDAOFactory(DaoFactory.MYSQL);
			PadreDao clientedao = dao.getPadreDao();
			PadreBean cliente = clientedao.BuscarApo(dni);
			System.out.println("INGRESA2");
			if(cliente != null){
				System.out.println("MANDA");
				
				String dndi=cliente.getDni();
				
				
			System.out.println("mandando datos al jsp");
				
				request.setAttribute("dniapo", dni);
				request.setAttribute("nombre", nom);
				request.setAttribute("apepat", pat);
				request.setAttribute("apemat", mat);
				request.setAttribute("grado", gra);
				request.setAttribute("seccion", sec);
				request.setAttribute("id", dndi);
				//request.setAttribute("diagnostico", cliente.getDiagnostico());				
				
		
				//response.sendRedirect("RegistroServi.jsp");
				RequestDispatcher rd = getServletContext().getRequestDispatcher("/Manten3rAlumno.jsp");
				rd.forward(request, response);
				
			}else{
				
				request.setAttribute("id", idPer);
				RequestDispatcher rd = getServletContext().getRequestDispatcher("/MantenerPadre.jsp");
				rd.forward(request, response);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
	}

}
